#!/bin/bash
set -eu
function dp_deploy() {
  tag=$2
  appName=$3
  setCpu=$4
  setMem=$5
  workspace=$6
  if [ ! -d ${workspace} ]; then
    mkdir -p ${workspace}
  fi
  cat <<EOF >/${workspace}/${appName}.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: paservice-frontend
  namespace: eworldcloud
  annotations:
    reloader.stakater.com/auto: "true"
spec:
  selector:
    matchLabels:
      tier: paservice-frontend
  replicas: 1
  template:
    metadata:
      labels:
        tier: paservice-frontend
    spec:
      containers:
      - name: paservice-frontend
        image: registry.mtywcloud.com/development/paservice-frontend:v$tag
        imagePullPolicy: IfNotPresent  # Always 开发时,用于Pod每次启动时都拉取镜像进行程序更新，生产环境用IfNotPresent
        resources:
          limits:
            cpu: "${setCpu}m"
            memory: "${setMem}Mi"
          requests:
            cpu: "100m"
            memory: "400Mi"
        ports:
        - name: http
          containerPort: 9000
        volumeMounts:
        - name: nginxconf
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
        - name: frontendconf
          mountPath: /etc/nginx/conf.d/
          readOnly: true
      volumes:
      - name: nginxconf
        configMap:
          name: nginxconf        
      - name: frontendconf
        configMap:
          name: frontendcommonconf
      serviceAccount: eworld-eworldcloud
      imagePullSecrets:
      - name: dev-harbor-secret
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 3
      maxUnavailable: 1
---
apiVersion: v1
kind: Service
metadata:
  name: paservice-frontend
  namespace: eworldcloud
spec:
  selector:
    tier: paservice-frontend
  ports:
    - protocol: TCP
      port: 9000
      targetPort: 9000
  type: ClusterIP
EOF
}
function main() {
  dp_deploy $*
}
case $1 in
"all")
  main $*
  ;;
"deploy_deployment")
  dp_deploy $*
  ;;
*)
  echo -e "\033[32m 参数如下: \033[0m"
  echo -e "\033[32m deploy_deployment \033[0m 部署App Deployment资源对象 后面跟参数"
  ;;
esac
